﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using TLDNs.Views;
using Prism.Ioc;
using Prism.Modularity;
using Prism.DryIoc;
using Prism.Mvvm;
using TLDNs.ViewModels;
using UsingCompositeCommands.Core;
using HandyControl.Data;
using HandyControl.Tools;


namespace TLDNs
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : PrismApplication
    {
        private ResourceDictionary UIL10N;

        protected override Window CreateShell()
        {
            if (TLDNs.Properties.Settings.Default.REC_MSG_Hz_ms >= 1)
            {
                ECANPort.CanComProc.REC_MSG_Hz_ms = TLDNs.Properties.Settings.Default.REC_MSG_Hz_ms;
            }
            else
            {
                ECANPort.CanComProc.REC_MSG_Hz_ms = 10;
            }

            if (TLDNs.Properties.Settings.Default.SEND_MSG_Hz_ms >= 1)
            {
                ECANPort.CanComProc.SEND_MSG_Hz_ms = TLDNs.Properties.Settings.Default.SEND_MSG_Hz_ms;
            }
            else
            {
                ECANPort.CanComProc.SEND_MSG_Hz_ms = 10;
            }

            Container.Resolve<AppResources>();
            UIL10N = (ResourceDictionary)this.Resources.MergedDictionaries[2];
            return Container.Resolve<TLDNs.Views.MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterSingleton<IAppCompositeCommands, AppCompositeCommands>();
            containerRegistry.RegisterForNavigation<CANInitConfigWindow>();


            containerRegistry.Register<AppUpdate>();
            containerRegistry.Register<ErrorHistory>();
            containerRegistry.Register<ParameterEdit>();
            containerRegistry.Register<ParameterEditTree>();
            containerRegistry.Register<ParameterExport>();
            containerRegistry.Register<ParameterMonitoring>();
            containerRegistry.Register<SystemInfo>();
            containerRegistry.Register<SystemInfoGraph>();
            containerRegistry.Register<TestFunctions>();


            //containerRegistry.RegisterForNavigation<AppUpdate>();
            //containerRegistry.RegisterForNavigation<ErrorHistory>();
            //containerRegistry.RegisterForNavigation<ParameterEdit>();
            //containerRegistry.RegisterForNavigation<ParameterEditTree>();
            //containerRegistry.RegisterForNavigation<ParameterExport>();
            //containerRegistry.RegisterForNavigation<ParameterMonitoring>();
            //containerRegistry.RegisterForNavigation<SystemInfo>();
            //containerRegistry.RegisterForNavigation<SystemInfoGraph>();
            //containerRegistry.RegisterForNavigation<TestFunctions>();

        }
        /// <summary>
        /// 模块加载
        /// </summary>
        /// <returns></returns>
        protected override IModuleCatalog CreateModuleCatalog()
        {
            return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };  // 配置文件
        }


        internal void ChangeLanguageEventSub(string language)
        {
            AppResources.Current.Language = language;
            string AsmName = typeof(MainWindowViewModel).Assembly.GetName().Name;
            Uri langUri = new Uri(@"pack://application:,,,/" + AsmName + ";component/Localization/" + language + ".xaml", UriKind.RelativeOrAbsolute);
            ResourceDictionary newDictionary = new ResourceDictionary();
            newDictionary.Source = langUri;

            if (UIL10N != null)
                UIL10N = newDictionary;

        }

        internal void UpdateSkin(SkinType skin)
        {
            var skins0 = Resources.MergedDictionaries[0];
            skins0.MergedDictionaries.Clear();
            var sk1 = ResourceHelper.GetSkin(skin);
            var sk2 = ResourceHelper.GetSkin(typeof(App).Assembly, "Res/Themes", skin);

            skins0.MergedDictionaries.Add(sk1);
            skins0.MergedDictionaries.Add(sk2);

            var skins1 = Resources.MergedDictionaries[1];
            skins1.MergedDictionaries.Clear();
            skins1.MergedDictionaries.Add(new ResourceDictionary
            {
                Source = new Uri("pack://application:,,,/HandyControl;component/Themes/Theme.xaml")
            });
            skins1.MergedDictionaries.Add(new ResourceDictionary
            {
                //TLDNs  TLDNs
                Source = new Uri("pack://application:,,,/TLDNs;component/Res/Themes/Theme.xaml")
            });
            try
            {
                var res = skins1.MergedDictionaries[0].MergedDictionaries[0].MergedDictionaries[0];
                var ie = res.Values.GetEnumerator();
                for (int i = 0; i < 29; i++)
                {
                    ie.MoveNext();
                }
                DeviceInfoIndex.FocusedBg = (System.Windows.Media.LinearGradientBrush)ie.Current;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Current.MainWindow?.OnApplyTemplate();
        }

        /// <summary>
        /// 自定义V与View绑定
        /// </summary>
        protected override void ConfigureViewModelLocator()
        {
            base.ConfigureViewModelLocator(); // 默认 AAA =>  AAAViewModel

            // type / type
            //ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), typeof(CustomViewModel));

            // type / factory
            //ViewModelLocationProvider.Register(typeof(MainWindow).ToString(), () => Container.Resolve<CustomViewModel>());

            // generic factory
            //ViewModelLocationProvider.Register<MainWindow>(() => Container.Resolve<CustomViewModel>());

            // generic type
            //ViewModelLocationProvider.Register<MainWindow, CustomMainWindowModel>();
        }



        public static void UIDispatcherDoAction(Action action)
        {
            var dispatcher = System.Windows.Application.Current.Dispatcher;
            if (dispatcher.CheckAccess())
            {
                action();
            }
            else
            {
                dispatcher.BeginInvoke(new Action(() =>
                {
                    action();
                }));
            }

        }
    }
}
